package foxnarabbit;

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.util.Random;

public class NonPlayer{

    private int dx;
    private int dy;
    private int x;
    private int y;
    private Image image;

    public NonPlayer(String fname){
        ImageIcon ii = new ImageIcon(fname);
        image = ii.getImage();
        x = randomX();
        y = randomY();
    }

    public int randomX(){
        Random rX = new Random();
        int newX = rX.nextInt() % 10;
        int possX[] = {20, 80, 140, 200, 260, 320, 380, 440, 500, 560};
        if(newX < 0)
            newX *= -1;
        return possX[newX];
    }

    public int randomY(){
        Random rY = new Random();
        int newY = rY.nextInt() % 10;
        int possY[] = {26, 56, 90, 122, 154, 186, 218, 250, 282, 314};
        if(newY < 0)
            newY *= -1;
        return possY[newY];
    }
    public void move(){
        x = x + dx;
        y = y + dy;
        dy = 0;
        dx = 0;
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public Image getImage() {
        return image;
    }

    public void keyPressed(int gotoX, int gotoY){
        int key = -1;
        
        if(x > gotoX)
            key = 0;
            else if(x < gotoX)
            key = 1;
            else if(y > gotoY)
            key = 2;
            else if( y < gotoY)
            key = 3;

         if (key == 0) {
            dx = -60;
            if(x == 20)
                dx = 0;
        }

        if (key == 1) {
            dx = 60;
            if(x == 560)
                dx = 0;
        }

        if (key == 2) {
            dy = -32;
            if(y == 26)
                dy = 0;
        }

        if (key == 3) {
            dy = 32;
            if(y == 314)
                dy = 0;
        }

    }

     public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }

}

